package board;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;

public class DataCommentAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		int dbno = Integer.parseInt(request.getParameter("dbno"));
		String mid = request.getParameter("mid");
		int cono = Integer.parseInt(request.getParameter("cono"));
		String dccomment = URLDecoder.decode(request.getParameter("dccomment"),"utf-8");
		
		DataCommentDAO dao = new DataCommentDAO();
		DataComment comment = new DataComment();
		comment.setDbno(dbno);
		comment.setMid(mid);
		comment.setDccomment(dccomment);
		comment.setIconno(1);
		
		dao.insert(comment);		
		int dcno = dao.getMaxDcno(comment);
		DataComment dComment = dao.getDataComment(dcno);		
		
		String nick = MemberCommunityDAO.getCommunityNick(mid, cono);
		
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");			
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();	
		
		
		out.println("<comment>");
		out.println("<dbno>"+dComment.getDbno()+"</dbno>");
		out.print("<dcno>" + dComment.getDcno() + "</dcno>");
		out.println("<id>" + dComment.getMid() + "</id>");				
		out.println("<message>" + dComment.getDccomment() + "</message>");
		out.println("<nick>"+nick +"</nick>");
		out.println("<day>" + dComment.getDcdate().substring(0, 19)+ "</day>");
		out.println("<iconno>"+dComment.getIconno()+"</iconno>");
		out.println("</comment>");
		out.close();
		
		System.out.println("content:"+comment.getDccomment());
		System.out.println(comment.getDcdate());
		System.out.println(comment.getMid());
		
		return null;
	}
	//특정 게시물 꼬리말 불러오기
//	public Vector commentList(DataComment dc, int cono){
//		DataCommentDAO comment = new DataCommentDAO();
//		int dbno = dc.getDbno();
//		Vector list = comment.getList(dbno);
//		
//		Vector commentList = new Vector();
//		
//		for(int i=0; i<list.size();i++){
//			DataCommentForm dcf = new DataCommentForm();
//			DataComment commentForm = (DataComment)list.elementAt(i);
//			dcf.setDbno(commentForm.getDbno());
//			dcf.setDcno(commentForm.getDcno());
//			dcf.setMid(commentForm.getMid());
//			dcf.setDccomment(commentForm.getDccomment());
//			dcf.setDcdate(commentForm.getDcdate().substring(0, 19));
//			
//			String nick = MemberCommunityDAO.getCommunityNick(commentForm.getMid(), cono);
//			dcf.setNick(nick!=null?nick:commentForm.getMid());
//			
//			commentList.add(dcf);
//		}
//		return commentList;
//	}
	
	public ActionForward getCommentList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		int dbno = Integer.parseInt(request.getParameter("dbno"));
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		DataCommentDAO dao = new DataCommentDAO();
		Vector<DataComment> coList = dao.getList(dbno);
		
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		
		for(int i=0;i<coList.size();i++){
			String nick = MemberCommunityDAO.getCommunityNick(coList.get(i).getMid(), cono);
			sb.append("<comment>");
			sb.append("<dbno>"+coList.get(i).getDbno()+"</dbno>");
			sb.append("<dcno>"+coList.get(i).getDcno()+"</dcno>");			
			sb.append("<mid>"+coList.get(i).getMid()+"</mid>");
			sb.append("<nick>"+nick+"</nick>");
			sb.append("<dccomment>"+coList.get(i).getDccomment()+"</dccomment>");
			sb.append("<dcdate>"+coList.get(i).getDcdate().substring(0,19)+"</dcdate>");
			sb.append("<iconno>"+coList.get(i).getIconno()+"</iconno>");
			sb.append("</comment>");			
		}
		sb.append("</comments>");
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);
		out.close();
		
		return null;
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		int dcno = Integer.parseInt(request.getParameter("dcno"));	
		System.out.println(dcno);
		
		DataComment comment = new DataComment();
		DataCommentDAO dao = new DataCommentDAO();		
		
		comment.setDcno(dcno);
		dao.delete(comment);		
		
//		PrintWriter out = response.getWriter();
//		
//		response.setContentType("text/xml");
//		response.setHeader("Cache-Control", "no-cache");
		
		return null;
	}

}
